<?php 
/**
 * This file is part of DomAr.
 * 
 * DomAr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DomAr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with DomAr.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @link http://inditel.ee/
 * @copyright 2007-2009 Inditel Meedia OÜ
 * @author Oliver Leisalu <oliver@inditel.ee>
 */


/**
 * This class uses Identity map pattern.
 *
 * @package DomAr
 */
class DomAr {
	
	/**
	* Objects that are stored for fast loading and not allowing two instances of same item.
	*/
	private static $objects = array();
	
	/**
	* Key in session to where cached objects are stored.
	*/
	const CACHED_OBJECTS_CACHE_KEY = 'DomAr_cached_objects';
	
	/**
	* Initializes DomAr.
	*/
	public static function init() {
		//self::$defaultDatabase = $database;
		if( !DomSql::getConnection( DomSql::DEFAULT_ALIAS ) )
			throw new RuntimeException('DomSql must be initialized before DomAr!');

	}


	/**
	* Loads object from stored objects or cached objects.
	* @param Int $id
	* @param String $className
	* @return DomArObject
	*/
	public static function getObject( $id, $className ) {
		
		$tableName = call_user_func( array($className, 'getTableName') );
		
		//is_a($className, 'DomArGlobalObject' )
		if( $className instanceof DomArGlobalObject && DomCache::isCached( DomAr::CACHED_OBJECTS_CACHE_KEY . '_' .  $tableName . '_' .  $id ))
			DomArObject::unserialize( DomCache::get( DomAr::CACHED_OBJECTS_CACHE_KEY . '_' .  $tableName . '_' . $id ) );
			//call_user_func( array($className, 'unserialize'), DomCache::get( DomAr::CACHED_OBJECTS_CACHE_KEY . '_' .  $className . '_' . $id ) );

			
		if( isset($_SESSION[self::CACHED_OBJECTS_CACHE_KEY][$className . '_' . $id]) === true ) {
			$dat = $_SESSION[self::CACHED_OBJECTS_CACHE_KEY][$className . '_' . $id];
			unset($_SESSION[self::CACHED_OBJECTS_CACHE_KEY][$className . '_' . $id]);
			DomArObject::unserialize( $dat );
			$_SESSION[self::CACHED_OBJECTS_CACHE_KEY][$className . '_' . $id] = $dat;
			//call_user_func( array(, 'unserialize'), $_SESSION[self::CACHED_OBJECTS_CACHE_KEY][$className . $id] );
		}
		
		if( isset(self::$objects[$tableName.'|'.$id]) )
			return self::$objects[$tableName.'|'.$id];
		else 
			return NULL;
	}
	
	/**
	* Stored object into stored objects list.
	* @param DomArObject $object
	*/
	public static function storeObject( DomArObject $object ) {
		if( ($id = $object->getId()) > 0)
			self::$objects[$object->getTableName().'|'.$id] = $object;
		else 
			throw new InvalidArgumentException('Can\'t store object without id!');
	}
	
	/**
	* Removes object from stored objects list
	* @param DomArObject $object
	*/
	public static function unStoreObject( DomArObject $object ) {
		unset( self::$objects[$object->getTableName().'|'.$object->getId()] );
	}
	
	/**
	* Removes all objects from stored objects list
	*/
	public static function flushStoredObjects() {
		self::$objects = array();
	}
	
	
	
	
	
	
	
	
	/**
	* Loads custom object data from database and returns it as array.
	* 
	* @param Int $id
	* @param String $className
	* @return Array
	* @deprecated
	*/
	public static function loadData( $id, $className ) {
		$tableName = call_user_func( array($className, 'getTableName') );
		return DomSql::selectOneQuery('SELECT * FROM `'.$tableName.'` WHERE id = '.(int)$id.' LIMIT 1');
	}
	
	/**
	* Loads custom object data from database. Use ClassName::load(id), not this one.
	* @param Int $id
	* @param String $className
	* @return DomArObject
	* @deprecated
	*/
	public static function load( $id, $className ) {
		
		$ob = self::getObject( $id, $className );
		if( $ob ) return $ob;

		$result = self::loadData( $id, $className);
		if( $result )
			return call_user_func_array( array($result['className'], 'create'), array(&$result) );
		else
			return NULL;
	}
	
}
?>